<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <div></div>
        <script>
            /*
             * 1.创建一个空对象
             * 2.链接到原型
             * 3.绑定this值
             * 4.返回新对象
             */
            // 第一种实现
            function createNew() {
                let obj = {}; // 1.创建一个空对象

                let constructor = [].shift.call(arguments);
                // let [constructor,...args] = [...arguments]

                obj.__proto__ = constructor.prototype; // 2.链接到原型

                let result = constructor.apply(obj, arguments); // 3.绑定this值
                // let result = constructor.apply(obj, args)

                // 对构造函数有返回值的判断处理
                return typeof result === 'object' ? result : obj; // 4.返回新对象
            }

            function People(name, age) {
                this.name = name;
                this.age = age;
                return 123;
            }

            let peo = createNew(People, 'Bob', 22);
            console.log(peo.name);
            console.log(peo.age);

            // 开始编码

            class Hello1 {
                constructor(name, age) {
                    this.name = name;
                    this.age = age;
                }
                say() {
                    console.log('say1...', this);
                }
            }

            function Hello(name, age) {
                this.name = name;
                this.age = age;
                function say() {
                    console.log('say...', this);
                }
                return 456;
            }
            // function mynew(construcor,...args) {
            function mynew(constructor, ...args) {
                console.log(arguments);
                console.log(Array.from(arguments));
                const news = {};
                news.__proto__ = constructor.prototype;
                let result = constructor.apply(news, ...args);
                return typeof result === 'object' ? result : obj; // 4.返回新对象
            }
            let my = mynew('Hello', 'tom', 28);
            console.log(my);
            console.log(my.name);
            console.log(my.age);
            console.log(my.__proto__);
            console.log(my.__proto__.construcor);
        </script>
    </body>
</html>
